package antani.client.pages;

import antani.client.GWTClient;
import antani.client.widgets.PlaylistItem;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PlayerPage extends Page {

	static PlayerPage instance;

	VerticalPanel playerPanel = new VerticalPanel();
	VerticalPanel playlistPanel = new VerticalPanel();
	Image cover = new Image();
	Label track = new Label("track");
	Label albumInfo = new Label("albuminfo");
	String currentCover = null;
	Panel mainPanel;

	protected int playlistIndex;

	public PlayerPage(Panel mainPanel) {

		instance = this;
		this.mainPanel = mainPanel;

		// layout

		VerticalPanel vp = new VerticalPanel();
		mainPanel.add(vp);

		HorizontalPanel menus = new HorizontalPanel();
		menus.setWidth("100%");
		vp.add(menus);

		playerPanel.setWidth("100%");
		vp.add(playerPanel);

		cover.addLoadHandler(new LoadHandler() {
			@Override
			public void onLoad(LoadEvent event) {
				cover.setPixelSize(150, 150);
				cover.setVisible(true);
			}
		});

		vp.add(playlistPanel);

		playerPanel.add(cover);
		// track.setStyleName("player_track");
		// playerPanel.add(track);
		// albumInfo.setStyleName("player_albuminfo");
		// playerPanel.add(albumInfo);

		HorizontalPanel controls = new HorizontalPanel();
		controls.setWidth("100%");
		playerPanel.add(controls);

		HorizontalPanel volumes = new HorizontalPanel();
		volumes.setWidth("100%");
		playerPanel.add(volumes);

		menus.add(new Button("albums", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				History.newItem("albums");
			}
		}));

		/*
		 * menus.add(new Button("timeline", new ClickHandler() {
		 * 
		 * @Override public void onClick(ClickEvent event) { History.newItem("timeline"); } }));
		 */
		menus.add(new Button("artists", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				History.newItem("artists");
			}
		}));

		menus.add(new Button("random", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				playRandomAlbum();
			}
		}));

		controls.add(new Button("prev", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				previous();
			}
		}));

		controls.add(new Button("stop", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				stop();
			}
		}));

		controls.add(new Button("next", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				next();
			}
		}));

		volumes.add(new Button("vol -", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				volDown();
			}
		}));

		volumes.add(new Button("vol +", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				volUp();
			}
		}));

	}

	@Override
	public void activate() {
		mainPanel.setVisible(true);
		getStatus();
	}

	@Override
	public void deactivate() {
		mainPanel.setVisible(false);
	}

	public void getStatus() {
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "/player?action=getStatus" + getUniqueParam());
		try {
			builder.sendRequest("", new RequestCallback() {

				@Override
				public void onResponseReceived(Request request, Response response) {
					String msg = response.getText();
					String[] lines = msg.split("\n");

					// remove old playlist
					playlistPanel.clear();

					// the album art unique id
					String url = lines[0];
					if (url.startsWith("http://") && !url.equals(currentCover)) {
						cover.setVisible(false);
						cover.setUrl(url);
						currentCover = url;
					}

					// the current index
					playlistIndex = 1 + Integer.parseInt(lines[1]);

					for (int i = 2; i < lines.length; i++) {

						String line = lines[i];
						if (line.length() > 0)
							playlistPanel.add(PlaylistItem.fromLine(i, line, playlistIndex == i));
					}
				}

				@Override
				public void onError(Request request, Throwable exception) {

				}
			});
		} catch (RequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void volDown() {
		String request = "/player?action=volDown";
		sendAndGetStatus(request);
	}

	public void volUp() {
		String request = "/player?action=volUp";
		sendAndGetStatus(request);
	}

	public void next() {
		String request = "/player?action=next";
		sendAndGetStatus(request);
	}

	public void previous() {
		String request = "/player?action=previous";
		sendAndGetStatus(request);
	}

	public void stop() {
		String request = "/player?action=stop";
		sendAndGetStatus(request);
	}

	public void jump(int index) {
		String request = "/player?action=jump&index=" + index;
		sendAndGetStatus(request);
	}

	public void playRandomAlbum() {
		String request = "/player?action=playRandomAlbum";
		sendAndGetStatus(request);
	}

	public void playAlbum(String album) {
		String request = "/player?action=playAlbum&id=" + album;
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, request + getUniqueParam());
		try {
			builder.sendRequest("", new RequestCallback() {

				@Override
				public void onResponseReceived(Request request, Response response) {
					GWTClient.goToPlayer();
				}

				@Override
				public void onError(Request request, Throwable exception) {

				}
			});
		} catch (RequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void sendAndGetStatus(String request) {
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, request + getUniqueParam());
		try {
			builder.sendRequest("", new RequestCallback() {

				@Override
				public void onResponseReceived(Request request, Response response) {
					getStatus();
				}

				@Override
				public void onError(Request request, Throwable exception) {

				}
			});
		} catch (RequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
